<template>
    <el-card class="card">
        <el-form :inline="true" :model="form" label-width="auto" style="max-width: 300px">
            <el-form-item label="角色名称：">
                <el-input placeholder="请输入关键字进行查询" v-model="form.name" />
            </el-form-item>
        </el-form>
        <el-button type="primary" style="margin-left: 10px">搜索</el-button>
    </el-card>

    <el-card>
        <div style="display: flex;">
            <el-button type="primary" style="margin-right: 10px" @click="showDialog">添加角色</el-button>
            <el-dialog v-model="dialogVisible" :title="roleInfo.id ? '更新角色' : '添加角色'" width="500">
                <el-form :model="roleInfo">
                    <el-form-item label="角色名称">
                        <el-input placeholder="请填写角色名称" v-model="roleInfo.roleName"></el-input>
                    </el-form-item>
                </el-form>
                <template #footer>
                    <div class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="confirm">
                            确定
                        </el-button>
                    </div>
                </template>
            </el-dialog>
            <!-- <el-button type="danger" style="margin-left: 10px">批量删除</el-button> -->
        </div>
        <el-table :data="tableData" style="width: 100%">
            <!-- <el-table-column type="index" label="序号" /> -->
            <el-table-column prop="id" label="id" />
            <el-table-column prop="roleName" label="角色名称" />
            <el-table-column prop="createTime" label="创建时间" />
            <el-table-column prop="updateTime" label="更新时间" />
            <el-table-column fixed="right" label="Operations" min-width="180px">
                <template #="{ row }">
                    <el-button type="primary" size="small" @click="clickAssignAcl(row)">分配权限</el-button>
                    <el-button type="primary" size="small" @click="updateRole(row)">编辑</el-button>
                    <el-button type="primary" size="small" @click="deleteSingle(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 100]"
            layout="prev, pager, next, sizes, total" :total="totalSize" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
    </el-card>

    <el-dialog v-model="dialogVisible2" title="分配菜单和按钮的权限" width="500">
        <el-tree ref="tree" style="max-width: 600px" :data="MenuList" show-checkbox node-key="id" default-expand-all
            :default-checked-keys="defaultKeys" :props="defaultProps" />
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible2 = false">取消</el-button>
                <el-button type="primary" @click="confirm2">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { addRoles, getRolesList, updateRolesInfo, deleteRoles, getMenuListByRole, setPermissionByRole } from '../../../api/user';
import { ElMessage } from 'element-plus';

const defaultProps = {
    children: 'children',
    label: 'name',
}
let tree = ref<any>()
const MenuList = ref<any>([])
const defaultKeys = ref<any>([])

const roleInfo = reactive<any>({
    roleName: '',
})

const roleInfo2 = reactive<any>({
    roleName: '',
    id: 0
})

const confirm = async () => {
    if (roleInfo.id) {
        await updateRolesInfo(roleInfo)
    } else {
        await addRoles(roleInfo)
    }
    dialogVisible.value = false
    getRolesLists()
    // window.location.reload() //加上，防止修改当前登录的用户名，就需要重新获取登录信息再展示，因此强制刷新一下即可
}
const updateRole = (row: any) => {
    dialogVisible.value = true
    Object.assign(roleInfo, row)
}

const deleteSingle = async (row: any) => {
    let result = await deleteRoles(row.id)
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '删除成功' })
        getRolesLists()
    }
}

const confirm2 = async () => {
    let roleId = roleInfo2.id
    let arr = tree.value.getCheckedKeys()
    let arr1 = tree.value.getHalfCheckedKeys()
    let result = await setPermissionByRole(roleId, arr.concat(arr1))
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '分配角色权限成功' })
        dialogVisible2.value = false
        getRolesLists()
        // window.location.reload() 
    }
}

const clickAssignAcl = async (row: any) => {
    dialogVisible2.value = true
    Object.assign(roleInfo2, row)
    let result = await getMenuListByRole(row.id)
    if (result.code == 200) {
        MenuList.value = result.data
        defaultKeys.value = filterSelectArr(MenuList.value, [])
    }
}

const filterSelectArr = (allData: any, inintArr: any) => {
    allData.forEach((item: any) => {
        if (item.select && item.level == 4) {
            inintArr.push(item.id)
        }
        if (item.children.length > 0) {
            filterSelectArr(item.children, inintArr)
        }
    });
    return inintArr
}

const dialogVisible = ref(false)
const dialogVisible2 = ref(false)

const showDialog = () => {
    dialogVisible.value = true
    Object.assign(roleInfo, {
        id: 0,
        roleName: '',
    })
}

let currentPage = ref(1)
let pageSize = ref(10)
let totalSize = ref(0)

const handleSizeChange = () => {
    getRolesLists()
}
const handleCurrentChange = () => {
    getRolesLists()
}

const form = reactive({
    name: ''
})

let tableData = ref<any>([])

const getRolesLists = async () => {
    let result = await getRolesList(currentPage.value, pageSize.value)
    if (result.code == 200) {
        totalSize.value = result.data.total
        tableData.value = result.data.records
    }
}

onMounted(async () => {
    await getRolesLists()
})

</script>

<style scoped lang="scss">
.card {
    :deep(.el-card__body) {
        display: flex;
    }
}
</style>